// pages/search-shop/searchshop.js
var api = require("../../api.js"),
    utils = require("../../utils.js"),
    app = getApp(),
    is_loading = !1,
    is_no_more = !1

Page({
  /**
   * 页面的初始数据
   */
  data: {
    init: false,
    keyword: '',   // 搜索内容
    latitude: '',
    longitude: '',
    pageCount: 0,
    page: 1,
    shopList: []   // 搜索结果列表
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.longitude && options.latitude) {
      this.setData({
        longitude: options.longitude,
        latitude: options.latitude
      })
    }
    if (options.keyword) {
      this.setData({
        keyword: options.keyword,
        shopList: []
      })
      // 加载搜索结果
      this.loadData()
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
  onReachBottom: function() {
    this.data.page > this.data.pageCount || this.loadMoreData();
  },
  /**
   * 加载搜索结果 
   */
  loadData() {
    const that = this,
          page = 1;
  wx.showLoading({
    title: "加载中"
  }), app.request({
    url: api.mch.shop_lists,
    data: {
      cat_id: 0,
      page: page,
      lat: that.data.latitude,
      log: that.data.longitude,
      keyword: that.data.keyword,
      type: 1
    },
    success: function(res) {
      0 == res.code && that.setData({
        shopList: res.data.list,
        pageCount: res.data.PageCount,
        page: page + 1
      });
    },
    fail: function(d) {
      wx.showToast({
        title: '获取商家失败',
        image: "http://runwu100.img.guoxiaoge.cn/icon-warning.png"
      });
    },
    complete: function(d) {
      that.setData({
        init: true
      })
      wx.hideLoading();
    }
  });
  },
  /**
   * 加载更多
   */
  loadMoreData: function() {
    var that = this,
      o = that.data.page;
    is_loading || (is_loading = !0, wx.showLoading({
      title: "加载中"
    }), app.request({
      url: api.mch.shop_lists,
      method: "GET",
      data: {
        cat_id: 0,
        page: o,
        lat: that.data.latitude,
        log: that.data.longitude,
        type: 1,
        keyword: that.data.keyword
      },
      success: function(t) {
        if (0 == t.code) {
          var a = that.data.shopList.concat(t.data.list);
          that.setData({
            shopList: a,
            pageCount: t.data.PageCount,
            page: o + 1
          });
        }
      },
      complete: function() {
        wx.hideLoading(), is_loading = !1;
      }
    }));
  },
})